package com.sinopec.scandevice.view

import android.os.Bundle
import android.view.Gravity
import com.alibaba.android.arouter.facade.annotation.Route
import com.jakewharton.rxbinding3.view.clicks
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.sinopec.basemodule.BaseApplication
import com.sinopec.basemodule.base.BaseActivity
import com.sinopec.basemodule.base.CustomToolbar
import com.sinopec.basemodule.dialog.ExitPopupWindow
import com.sinopec.basemodule.utils.ARouterManagerUtils
import com.sinopec.scandevice.R
import com.sinopec.scandevice.contract.MainContract
import com.sinopec.scandevice.presenter.MainPresenter
import io.reactivex.android.schedulers.AndroidSchedulers
import kotlinx.android.synthetic.main.activty_main.*
import java.util.concurrent.TimeUnit

/**
 * @author lwj
 * @ClassName: MainActivity
 * @Description: 主界面
 */
@Route(path = ARouterManagerUtils.GOTO_LM_MAIN_ACTIVITY)
class MainActivity : BaseActivity<MainPresenter?>(), MainContract.View {

    //退出登录弹窗
    private lateinit var exitPopupWindow: ExitPopupWindow

    override fun initView(savedInstanceState: Bundle?) {
        mPresenter = MainPresenter(this)
        val userInfo = BaseApplication.getInstance().userManager.user
        srl.setEnableAutoLoadMore(false)
        //刷新控件
        srl.setOnRefreshListener { refreshlayout: RefreshLayout ->
            refreshlayout.finishRefresh()
        }
        //设备id
        tv_device_id.text = userInfo.deviceId
        //仓库
        tv_warehouse.text = userInfo.stockWarehouseName
        //登录用户
        tv_name.text = userInfo.userAccount
        //设置版本号
        app_version_tv.text = BaseApplication.getInstance().versionName
        //获取数据
       // srl.autoRefresh()
    }

    override fun getContentView(): Int {
        return R.layout.activty_main
    }

    override fun configToolbar(rToolbar: CustomToolbar) {
        rToolbar.setToolbarVisible(false)
    }

    override fun onResume() {
        super.onResume()
        //srl.autoRefresh()
    }

    override fun initEvent() {
        super.initEvent()

        /**
         * 设置
         */
        addDisposable(ll_set.clicks()
            .throttleFirst(1, TimeUnit.SECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                ARouterManagerUtils.gotoSetActivity()
            })

        /**
         * 退出
         */
        addDisposable(ll_login_out.clicks()
            .throttleFirst(1, TimeUnit.SECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                showExitPopupWindow()
            })

        /**
         * 入库tab
         */
        addDisposable(ll_tab_stockIn.clicks()
            .throttleFirst(1, TimeUnit.SECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                //入库列表
                ARouterManagerUtils.gotoStockInListActivity()
            })


        /**
         * 出库tab
         */
        addDisposable(ll_tab_stockOut.clicks()
            .throttleFirst(1, TimeUnit.SECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                //出库列表
                ARouterManagerUtils.gotoStockOutListActivity()
            })

        /**
         * 检查更新
         */
        addDisposable(app_version_ll.clicks()
            .throttleFirst(1, TimeUnit.SECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                BaseApplication.getInstance().appUpdateEvent(true)
            })

    }


    /**
     * @author lwj
     * @Description: 退出登录弹窗
     */
    private fun showExitPopupWindow() {
        exitPopupWindow = ExitPopupWindow.create(mContext) {
            exitPopupWindow.dismiss()
            ARouterManagerUtils.gotoLoginActivity()
            BaseApplication.getInstance().exit()
            mContext.finish()
        }.setAnchorView(mContext.rootView)
            .apply()
        exitPopupWindow.showAtLocation(mContext.rootView, Gravity.CENTER, 0, 0)
    }
}